﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using TBM.Filters;
using TBM.Attributes;

namespace TBM.Controllers
{
    public partial class SystemController : MasterController
    {
        [Description("菜单配置")]
        [TBMAuthorize("系统配置", "菜单配置")]
        public ActionResult MenuSetting()
        {
            List<TBM.Models.MainMenu> mainMenu = new List<TBM.Models.MainMenu>();
            List<TBM.Models.MainMenu> pageList = new List<Models.MainMenu>();
            cmd.CommandText = "select ID, PID, PageName, SubTitle, URL, Icon, IsShow, Descr, Sort from dbo.Pages order by Sort";
            cmd.CommandType = CommandType.Text;

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    pageList.Add(new Models.MainMenu
                    {
                        ID = (int)dr["ID"],
                        PID = (int)dr["PID"],
                        PageName = dr["PageName"].ToString(),
                        SubTitle = dr["SubTitle"].ToString(),
                        URL = dr["URL"].ToString(),
                        Icon = dr["Icon"].ToString(),
                        IsShow = (bool)dr["IsShow"],
                        Descr = dr["Descr"].ToString(),
                        Sort = (int)dr["Sort"]
                    });
                }

                HttpRuntime.Cache.Insert("PageList", pageList);
            }

            foreach (TBM.Models.MainMenu item in pageList.Where(a => a.PID == 0))
            {
                mainMenu.Add(item);
                AddSubMenu(item, pageList);
            }

            ViewBag.MenuList = mainMenu;

            return PartialView();
        }

        /// <summary>
        /// 添加子菜单
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="pageList"></param>
        private void AddSubMenu(Models.MainMenu parent, List<TBM.Models.MainMenu> pageList)
        {
            List<TBM.Models.MainMenu> subList = pageList.Where(a => a.PID == parent.ID).ToList();

            if (subList.Count == 0) return;

            foreach (TBM.Models.MainMenu item in subList)
            {
                AddSubMenu(item, pageList);
            }

            parent.SubMenu = subList;
        }


        [ParentAction("MenuSetting")]
        [Description("更新菜单信息")]
        [TBMAuthorize("系统配置", "更新菜单信息")]
        public JsonResult UpdateMenu(int[] id, int[] pid, int[] sort, string[] pagename, string[] subtitle, string[] icon, bool[] isshow, string[] url, string[] descr)
        {
            JsonResult result = new JsonResult();

            cmd.CommandText = @"update dbo.Pages
	                            set PID = @PID, PageName = @PageName, SubTitle = @SubTitle, URL = @URL,
		                            Icon = @Icon, IsShow = @IsShow, Descr = @Descr, Sort = @Sort
	                            where
		                            ID = @ID";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@ID", SqlDbType.Int);
            cmd.Parameters.Add("@PID", SqlDbType.Int);
            cmd.Parameters.Add("@PageName", SqlDbType.NVarChar, 10);
            cmd.Parameters.Add("@SubTitle", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@URL", SqlDbType.VarChar, 100);
            cmd.Parameters.Add("@Icon", SqlDbType.VarChar, 20);
            cmd.Parameters.Add("@IsShow", SqlDbType.Bit);
            cmd.Parameters.Add("@Descr", SqlDbType.NVarChar, 100);
            cmd.Parameters.Add("@Sort", SqlDbType.Int);

            for (int i = 0; i < id.Length; i++)
            {
                cmd.Parameters["@ID"].Value = id[i];
                cmd.Parameters["@PID"].Value = pid[i];
                cmd.Parameters["@PageName"].Value = pagename[i];
                cmd.Parameters["@SubTitle"].Value = subtitle[i];
                cmd.Parameters["@URL"].Value = url[i];
                cmd.Parameters["@Icon"].Value = icon[i];
                cmd.Parameters["@IsShow"].Value = isshow[i];
                cmd.Parameters["@Descr"].Value = descr[i];
                cmd.Parameters["@Sort"].Value = sort[i];
                cmd.ExecuteNonQuery();
            }

            result.Data = new Models.ExecuteResult
            {
                Result = true,
                Code = Core.Lang.CMD_SUCCESS.Code,
                Message = Core.Lang.CMD_SUCCESS.Message
            };

            return result;
        }
    }
}
